package org.zjx.service;

import org.zjx.common.bean.Item;
import org.zjx.common.entity.Address;
import org.zjx.common.entity.Inventor;
import org.zjx.common.entity.Quest;
import org.zjx.common.service.ActionResultService;
import org.zjx.server.annotation.RpcService;
import org.zjx.state.InventorState;
import org.zjx.state.PlayerState;
import org.zjx.state.QuestState;
import org.zjx.state.StateManager;

import java.util.ArrayList;
import java.util.List;


@RpcService(ActionResultService.class)
public class ActionResultServiceImpl implements ActionResultService {
    @Override
    public boolean receiveQuest(String playerId, Quest quest) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return false;
        QuestState questState = playerState.getQuestState();
        if (questState == null){
            questState = new QuestState();
            questState.setQuests(new ArrayList<>());
            playerState.setQuestState(questState);
        }
        questState.getQuests().add(quest);
        return true;
    }

    @Override
    public boolean submitQuest(String playerId, Quest quest) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return false;
        QuestState questState = playerState.getQuestState();
        if (questState == null){
            questState = new QuestState();
            questState.setQuests(new ArrayList<>());
            playerState.setQuestState(questState);
        }
        questState.getQuests().remove(quest);
        return true;
    }

    @Override
    public boolean move(String playerId, Address address) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null || address == null) return false;
        playerState.setAddress(address);
        playerState.setStandStartTime(System.currentTimeMillis());
        return true;
    }

    @Override
    public boolean useItem(String playerId, int barSort, int index) {
        return false;
    }

    @Override
    public boolean buyItem(String playerId, Item item, int number, String attribute) {
        return false;
    }

    @Override
    public boolean handoffBar(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null ) return false;
        InventorState inventorState = playerState.getInventorState();
        if (inventorState == null) return false;
        List<Inventor> inventors = inventorState.getSlot();
        inventorState.setSlot(inventorState.getBag());
        inventorState.setBag(inventors);
        return true;
    }
}
